<script setup>
// tab列表
import {getOrderByStatusAPI, toGetOrderAPI} from "@/apis/goodsOrder.js";
import {useAliPayAPI} from "@/apis/pay.js";
import {useCountDown} from "@/utils/useCountDown.js";
import {ElMessage} from "element-plus";
const {formatTime,start} = useCountDown();
const tabTypes = [
  { name: -1, label: "全部订单" },
  { name: 1, label: "待付款" },
  { name: 2, label: "待发货" },
  { name: 3, label: "待收货" },
  { name: 4, label: "待评价" },
  { name: 5, label: "已完成" },
  { name: 0, label: "已取消" }
]
const activeName = ref(-1)
// 订单列表
const orderList =ref([])
//列表总数
const total = ref(0)
const params = ref({
  pageSize: 3,
  current: 1,
  orderStatus: -1
})

const toAliPay = async (orderId) => {
  /*window.open('http://localhost:8888/api/pay/aliPay?orderId=' + route.query.id)*/
  const res = await useAliPayAPI(orderId)
  console.log(res)
  document.write(res.data)
}
const toGetGoods = async (orderId) => {
  const res = await toGetOrderAPI(orderId)
  if(res.data){
    ElMessage.success("收货成功")
    await getOrderList()
  }
}
const getOrderList = async () => {
  const res = await getOrderByStatusAPI(params.value)
  orderList.value = res.data.records
  total.value = res.data.total
}
onMounted( () => getOrderList())
const changeActive = () => {
  params.value.orderStatus = activeName.value
  //页数要切换回来
  getOrderList()
}
// 页数切换
const pageChange = (current) => {
  if (params.value.pageSize * current > total.value) {
    params.value.current = 1;
  }else{
    console.log("page",current)
    params.value.current = current
  }
  getOrderList()
}

//记录数切换
const sizeChange = (size) => {
  params.value.pageSize = size
  getOrderList()
}
</script>

<template>

  <div class="order-container">
    <el-tabs v-model="activeName" @tab-change="changeActive" >
      <!-- tab切换 -->
      <el-tab-pane v-for="(item,index) in tabTypes" :key="index" :label="item.label" :name="item.name"/>
      <div class="main-container">
        <div class="holder-container" v-if="orderList.length === 0">
          <el-empty description="暂无订单数据" />
        </div>
        <div v-else>
          <!-- 订单列表 -->
          <div class="order-item" v-for="(order,index) in orderList" :key="index">
            <div class="head">
              <span>下单时间：{{ order.createTime }}</span>
              <span>订单编号：{{ order.orderId }}</span>
              <!-- 未付款，倒计时时间还有 -->
              <span class="down-time" v-if="order.orderStatusMap['status'] === '1'">
                <i class="iconfont icon-down-time"></i>
                <b v-if="order.countDownTime >0">付款截止: {{order.payLastTime}}</b>
              </span>
            </div>
            <div class="body">
              <div class="column goods">
                <ul>
                  <li v-for="item in order.skusDetailList" :key="item.id">
                    <a class="image" href="javascript:;">
                      <img :src="item.goodsImage" alt="" />
                    </a>
                    <div class="info">
                      <p class="name ellipsis-2">
                        {{ item.goodsName }}
                      </p>
                      <p class="attr ellipsis">
                        <span>{{ item.attrsText }}</span>
                      </p>
                    </div>
                    <div class="price">¥{{ item.skuTotalPrice?.toFixed(2) }}</div>
                    <div class="count">x{{ item.quantity }}</div>
                  </li>
                </ul>
              </div>
              <div class="column state">
                <p>{{order.orderStatusMap["statusName"]}}</p>
                <p v-if="order.orderStatusMap['status']=== '3'">
                  <a href="javascript:;" class="green">查看物流</a>
                </p>
                <p v-if="order.orderStatusMap['status'] === '4'">
                  <a href="javascript:;" class="green">评价商品</a>
                </p>
                <p v-if="order.orderStatusMap['status'] === '4'">
                  <a href="javascript:;" class="green">查看评价</a>
                </p>
                <p v-if="order.orderStatusMap['status'] === '0'">
                  <a href="javascript:;" class="green">已取消</a>
                </p>
              </div>
              <div class="column amount">
                <p class="red">¥{{ order.totalPrice?.toFixed(2) }}</p>
                <p>（含运费：¥{{ order.postage?.toFixed(2) }}）</p>
                <p>在线支付</p>
              </div>
              <div class="column action">
                <el-button  v-if="order.orderStatusMap['status'] === '1'&&order.countDownTime>0" type="primary"
                            size="small" @click="toAliPay(order.orderId)">
                  立即付款
                </el-button>
                <el-button v-if="order.orderStatusMap['status'] === '3'" type="primary" size="small" @click="toGetGoods(order.orderId)" >
                  确认收货
                </el-button>
              </div>
            </div>
          </div>
          <!-- 分页 -->
          <div class="pagination-container">
              <el-pagination
                  background
                  layout="prev, pager, next, sizes"
                  :total="total"
                  :page-size="params.pageSize"
                  @current-change="pageChange"
                  :page-sizes="[2,5,10]"
                  @size-change="sizeChange"
              />
          </div>
        </div>
      </div>
    </el-tabs>
  </div>
</template>

<style scoped lang="scss">
.order-container {
  padding: 10px 20px;

  .pagination-container {
    display: flex;
    justify-content: center;
  }

  .main-container {
    min-height: 500px;

    .holder-container {
      min-height: 500px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      >p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: $xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: $priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }
}
</style>